package com.hxut.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@WebServlet(name = "CookieServlet",urlPatterns = "/cookieServlet")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("city", "wuhan");
        Cookie cookie1 = new Cookie("address", "hxut");
        Cookie cookie2 = new Cookie(URLEncoder.encode("城市","utf-8"), URLEncoder.encode("武汉","utf-8"));

        //有效时长30秒
        //cookie.setMaxAge(30);

        //URLEncoder.encode("",""):编码
        //URLDecoder.decode("",""):解码
        resp.addCookie(cookie);
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

}
